Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow LinePlot to be drawn in steps #99

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

larshei
Copy link

@larshei larshei commented Aug 12, 2024

Changes

Many timeseries depict a "state" of some sort that was measured at specific points in time.

Simply drawing a line between the points of measurement often feels weird to me. I rather think "the value has been y from t1 to t2, then at t2 we got a new value".

So I added a style for line graphs that creates a LinePlot with steps:

image

As we no longer have two options (smooth yes/no) but three options,
the interface needed to be changed:

The option is now called plot_type, with the options

  • :direct
  • :smooth
  • :step

If plot_type is not provided, we fall back to the smoothed option.

@mindok
Copy link
Owner

mindok commented Aug 14, 2024

Thanks @larshei - it looks great!

It is a breaking change to the API so what I might do is add back the :smoothed keyword option and map it into your new :plot_style option behind the scenes and note the :smoothed option as deprecated in the documentation.

Edit: Just saw the last commit 🙄

# keep backwards compatibility with old `:smoothed` option
style = get_option(plot, :plot_style) |> IO.inspect(label: "style")
smoothed = get_option(plot, :smoothed) |> IO.inspect(label: "smoothed")
plot_style = if smoothed != nil, do: smoothed, else: style
Copy link
Owner

@mindok mindok Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be:

plot_style = if smoothed != nil, do: :smooth, else: style

(i.e. :smoothed atom set)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - I see what you've done - the true/false is handled in the path logic. I think it's better to definitively set the plot_style here for a couple of reasons:

  1. We isolate the deprecation logic to a single spot
  2. The paths code may be used from elsewhere in the future and the options (true, false, :smooth, :direct, :step) are overlapping and unclear
  3. There are fewer places to go to remove the deprecated code

so something like:

  plot_style = case smooth do
    true -> if smoothed, do: :smooth, else: :direct
    _ -> style
  end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted to

    plot_style =
      case smoothed do
        true -> :smooth
        false -> :direct
        _ -> style
      end

lib/chart/svg.ex Outdated
@@ -85,7 +85,11 @@ defmodule Contex.SVG do

defp path([], _), do: ""

defp path(points, false) do
defp path(points, nil), do: path(points, :smooth)
defp path(points, true), do: path(points, IO.inspect(:smooth))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to remove the calls to IO.inspect() once we're happy with the changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the whole distinction on the function level and moved the mapping to the calling function, as you suggested above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants